// pages/wait_evaluate/wait_evaluate.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util'), WeValidator = require('../../utils/we-validator');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text_num: 0,
    star1: 0,
    star2: 0,
    star3: 0,
    content: '',
    selectedTags: [],
    startTexts: ['','非常差','差','一般','好','非常好'],
  },
  check: function (e) {
    this.setData({
      switch2Checked: !this.data.switch2Checked
    })
  },
  input: function (e) {
    this.setData({
      text_num: e.detail.value.length, content: e.detail.value.trim()
    })
  },
  star1: function (e) {
    this.setData({
      star1: e.currentTarget.dataset.index + 1
    })
  },
  star2: function (e) {
    this.setData({
      star2: e.currentTarget.dataset.index + 1
    })
  },
  star3: function (e) {
    this.setData({
      star3: e.currentTarget.dataset.index + 1
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenerEventChannel().on('dataTransfer', data=>{
      this.setData({item: data.interview});
      util.getCommentTags().then(tags=>this.setData({tags}));
    });
  },
  bindSelectTag(event) {
    let tag = event.currentTarget.dataset.tag;
    let index = this.data.selectedTags.indexOf(tag);
    if(index>-1) {
      this.data.selectedTags.splice(index, 1);
    }else {
      this.data.selectedTags.push(tag);
    }
    this.setData({selectedTags: this.data.selectedTags});
  },
  bindSubmit(event) {
    let data = {
      interview_id: this.data.item.id,
      environment_score: this.data.star1||'',
      benefit_score: this.data.star2||'',
      opportunity_score: this.data.star3||'',
      tags: this.data.selectedTags.join(','),
      content: this.data.content,
      anonymous: this.data.switch2Checked ? 1:0,
    };
    let validator = new WeValidator({
      rules: {
        environment_score: {required: true},
        benefit_score: {required: true},
        opportunity_score: {required: true},
        // content: {required:true}
      },
      messages: {
        environment_score: {required: '请对办公环境评分'},
        benefit_score: {required: '请对企业福利评分'},
        opportunity_score: {required: '请对晋升空间评分'},
        // content: {required:'请填写评论内容'}
      }
    });
    if(!validator.checkData(data)) {
      return;
    }
    core.post({
      url: 'xilujob.interview_comment/add_comment', data: data,
      success: ret => {
        this.getOpenerEventChannel().emit('dataTransfer', {interview: this.data.item});
        wx.navigateBack();
      }
    });
  }


});
